<?php
/**
 * Created by PhpStorm.
 * User: 86158
 * Date: 2021/6/14
 * Time: 12:12
 */

namespace App\Models\user;


use App\Models\Model;
use App\Models\User;

class SportLog extends Model
{
    protected $fillable = [
        'user_id',
        'number'
    ];

    public function user()
    {
        return $this->hasOne(User::class,'id','user_id');
    }

    public function add($data)
    {
        return self::firstOrCreate($data);
    }

    public function rankList()
    {
        $start_time = date("Y-m-d",time());
        $data = self::where('created_at','>=',$start_time)
            ->groupBy('user_id')
            ->with('user')
            ->select([\DB::raw("sum(number) as total_number"),'user_id'])
            ->orderByDesc('total_number')
            ->get()->toArray();
        return $data;
    }
}